<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/15
 * Time: 下午3:57
 */

namespace Data\Company;


use Core\Model;

class CompanyCatlogModel extends Model
{
    protected $table = 'company_catlog';

    /**
     * @return bool|mixed
     */
    public function setCache(){
        $catloglist = $this->select();
        $datalist = array();
        foreach ($catloglist as $catlog){
            $datalist[$catlog['catid']] = $catlog;
        }

        return cache('company_catlogs', $datalist);
    }

    /**
     * @return bool|mixed
     */
    public function getCache(){
        $catloglist = cache('company_catlogs');
        if (is_array($catloglist)) {
            return $catloglist;
        }else {
            $this->setCache();
            return $this->getCache();
        }
    }

    /**
     * 获取当前分类ID及所有子类ID
     * @param $catid
     * @param array $childids
     * @return array
     */
    public function getAllChildids($catid, &$childids=array()){
        static $catlogList;
        if (!$catlogList) {
            $catlogList = $this->getCache();
        }
        if (!$childids) {
            $childids = array($catid);
        }

        foreach ($catlogList as $catlog){
            if ($catlog['fid'] == $catid) {
                $childids[] = $catlog['catid'];
                $this->getAllChildids($catlog['catid'], $childids);
            }
        }
        return $childids;
    }

    /**
     * 获取目录树
     * @return array
     */
    public function getCatlogTree(){
        $catloglist = array();
        foreach ($this->getCache() as $catlog){
            $catloglist[$catlog['fid']][$catlog['catid']] = $catlog;
        }
        return $catloglist;
    }

    /**
     * 获取当前分类及所有子类信息
     * @param $catid
     * @param array $childCatlog
     * @return array
     */
    public function getAllChilds($catid, &$childCatlog=array()){
        static $catloglist;
        if (!$catloglist) $catloglist = $this->getCache();
        if (!$childCatlog) $childCatlog[] = $catloglist[$catid];
        foreach ($catloglist as $catlog){
            if ($catlog['fid'] == $catid){
                $childCatlog[] = $catlog;
                $this->getAllChilds($catlog['catid'], $childCatlog);
            }
        }
        return $childCatlog;
    }

    /**
     * 获取当前分类及所有父级分类ID
     * @param $catid
     * @param $parentCatids
     * @return mixed
     */
    public function getParentIds($catid, &$parentCatids=array()){
        static $catloglist;
        if (!$catloglist) $catloglist = $this->getCache();
        if (!$parentCatids) $parentCatids = array($catid);

        $curCatlog = $catloglist[$catid];
        if ($curCatlog['fid']) {
            foreach ($catloglist as $catlog){
                if ($catlog['catid'] == $curCatlog['fid']){
                    $parentCatids[] = $catlog['catid'];
                    $this->getParentIds($catlog['catid'], $parentCatids);
                }
            }
        }
        return $parentCatids;
    }

    /**
     * 获取当前分类及所有父级分类
     * @param $catid
     * @param array $parents
     * @return array
     */
    public function getParents($catid, &$parents=array()){
        static $catloglist;
        if (!$catloglist) $catloglist = $this->getCache();

        $curCatlog = $catloglist[$catid];
        if (!$parents) $parents = array($curCatlog);
        if ($curCatlog['fid']) {
            foreach ($catloglist as $catlog){
                if ($catlog['catid'] == $curCatlog['fid']){
                    $parents[] = $catlog;
                    $this->getParents($catlog['catid'], $parents);
                }
            }
        }
        return $parents;
    }
}